( function($) {"use strict";
        $.extend({
            "Xslide" : function(opt) {
                var options = $.extend({
                    rootElement : ".J-Xslide",
                    hd : ".Xslide-hd",
                    bd : ".Xslide-bd",
                    container : ".Xslide-container",
                    item : ".Xslide-item",
                    eve : "mouseover",
                    controlElement : ".Xslide-pageControl",
                    autoPlay : false,
                    autoTimer : 3000,
                    triggerIndex : 1,
                    effect : "fade",
                    pageControl : false
                }, opt),
                //isIE6 = !-[1,] && !window.XMLHttpRequest,
                i, itemLen = $(options.item).length, html = "<ul>", timeoutId = null, hd = $(options.hd), bd = $(options.bd), effect,
                Obj=null,
                instance;
				
			
			
                //---------------------
                // Directions	: 效果
                //---------------------
				/*
                effect = function(e, tabIndex) {
                    switch(options.effect) {
                        case "none":
                            effect = function(e, tabIndex) {
                                hd.bind("change.tabs", function(e, tabIndex) {
                                    bd.find(options.item).hide();
                                    $(bd.find(options.item).get(tabIndex)).show();
                                });
                            };
                            break;
                        case "fade":
                            effect = function(e, tabIndex) {
                                hd.bind("change.tabs", function(e, tabIndex) {
                                    bd.find(options.item).fadeOut();
                                    $(bd.find(options.item).get(tabIndex)).stop(true, true).fadeIn();
                                });
                            };
                            break;
                        case "slideDown":
                            effect = function(e, tabIndex) {
                                hd.bind("change.tabs", function(e, tabIndex) {
                                    bd.find(options.item).hide();
                                    $(bd.find(options.item).get(tabIndex)).stop(true, true).slideDown(300);
                                });
                            };
                            break;
                        case "slideUp":
                            effect = function(e, tabIndex) {
                                hd.bind("change.tabs", function(e, tabIndex) {
                                    bd.find(options.item).hide();
                                    $(bd.find(options.item).get(tabIndex)).stop(true, true).slideDown(300);
                                });
                            };
                            break;
                        case "slideLeft":
                            effect = function(e, tabIndex) {
                                hd.bind("change.tabs", function(e, tabIndex) {
                                    bd.find(options.item).hide();
                                    $(bd.find(options.item).get(tabIndex)).stop(true, true).slideDown(300);
                                });
                            };
                            break;
                        default :
                            effect = function(e, tabIndex) {
                                hd.bind("change.tabs", function(e, tabIndex) {
                                    bd.find(options.item).hide();
                                    $(bd.find(options.item).get(tabIndex)).stop(true, true).slideDown(300);
                                });
                            };
                    }
                    return effect;
                };
				*/

				
				//---------------------
				// Directions	: pageControl  上一页 下一页
				//---------------------
                if (options.pageControl === false) {
                    $(options.controlElement).hide();
                }

                //---------------------
                // Directions	: make hd item
                //---------------------
                for ( i = 0; i < itemLen; i++) {
                    html += "<li>" + (i+1) + "</li>";
                }
                html += "</ul>";
                $(options.hd).append(html);

				Obj =function(){
					 //---------------------
					// Directions	: rootElement hover class
					//---------------------
					$(options.rootElement).bind("mouseenter", function() {
						$(this).addClass("hover");
					});
					$(options.rootElement).bind("mouseleave", function() {
						$(this).removeClass("hover");
					});
					hd.delegate("li", "mouseenter", function() {
						var tabIndex = $(this).index();
						hd.trigger("change.tabs", tabIndex);
					});

					//---------------------
					// Directions	: //给element绑定一个change.tabs自定义事件
					//---------------------
					hd.bind("change.tabs", function(e, tabIndex) {
						hd.find("li").removeClass("on");
						$(hd.find("li").get(tabIndex)).addClass("on");
						//console.log("激活了" + tabIndex);
					});

					hd.bind("change.tabs", function(e, tabIndex) {
						bd.find(options.item).hide();
						$(bd.find(options.item).get(tabIndex)).show();
						//effect(e, tabIndex);
					});
				};

				Obj.prototype = {
					playNext : function(){
						//console.log("playNext");
					},
					playPrev : function(){
						//console.log("playPrev");
					},
					playFirst : function(){
						//console.log("playFirst");
					}
				};
				instance = new Obj();

				
               

                //---------------------
                // Directions	: auto play 自动播放
                //---------------------
                if (options.autoPlay === true) {

					(function() {
						var index = 0;
						function autoPlay() {
							//console.log("autoPlay index: " + index);
							hd.trigger("change.tabs", index);
							index += 1;
							if (index >= itemLen) {
								index = 0;
							}
							
							timeoutId = setTimeout(autoPlay, options.autoTimer);
						}

						autoPlay();
						
						$(options.rootElement).bind("mouseenter", function() {
							
							clearTimeout(timeoutId);
						});

						$(options.rootElement).bind("mouseleave", function(e) {
							//console.log(timeoutId);
							//index = 1;
							//console.log(index);
							//console.log("mouseleave index: " + index);
							clearTimeout(timeoutId);
							timeoutId = null;
							autoPlay();
						});

					}());

                } else {
                    //激活第一个选项卡
                    hd.trigger("change.tabs", options.triggerIndex);
                }


            }
        });
    }(jQuery)); 